-- local PlayerCharacter={}

-- function FullPlayerCharacter(userId,callBack)
-- 	if not userId then
-- 		callBack(nil,nil)
-- 	end
-- 	if not PlayerCharacter[userId] then
-- 		local character,s
-- 		s=pcall(function()
-- 			character= game.Players:CreateHumanoidModelFromUserId(userId)
-- 		end)
-- 		if s  then
-- 			PlayerCharacter[userId]=character
-- 			if callBack then
-- 				callBack(character:Clone())
-- 			end
-- 			return 
-- 		else
-- 			for i=1,2 do
-- 				task.wait(0.2)
-- 				s=pcall(function()
-- 					character= game.Players:CreateHumanoidModelFromUserId(userId)
-- 				end)
-- 				if s then
-- 					PlayerCharacter[userId]=character
-- 					if callBack then
-- 						callBack(character:Clone())
-- 					end
-- 					return 
-- 				end
-- 			end
-- 		end
-- 		character=_G.CommonOHOH:GetPath_Loop("Friend",game.ReplicatedStorage.Resource):Clone()

-- 		_G.ClientOHOH.load.PlayerInfoHelper().GetPlayerInfo(userId,function(head,name)
-- 			_G.CommonOHOH:GetPath_Loop("UpperTorso.Head",character).Texture=head
-- 		end)
		
-- 		PlayerCharacter[userId]=character
-- 		if callBack then
-- 			callBack(character:Clone())
-- 		end
-- 	else

-- 		if callBack then
-- 			callBack(PlayerCharacter[userId]:Clone())
-- 		end
-- 	end
-- end

-- return {
-- 	GetPlayerCharacter=function(userId,bk)
-- 		FullPlayerCharacter(userId,bk)
-- 	end,
-- }

local CommonOHOH = _G.CommonOHOH;

local PlayerCharacter={}
local CallBackList={}
local UpdateConnect=nil

local M={}

function M.Update()

	UpdateConnect = coroutine.wrap(function()
		while true do
			-- print("---UUUUUU--character--loop--->")
			local kId=nil
			for k,v in pairs(CallBackList) do
				local character
				local s,e=xpcall(function()
					 character= game.Players:CreateHumanoidModelFromUserId(k)
				end,debug.traceback)

				if not s then
					character=_G.CommonOHOH:GetPath_Loop("Friend",game.ReplicatedStorage.Resource):Clone()
					_G.ClientOHOH.load.PlayerInfoHelper().GetPlayerInfo(k,function(head,name)
						_G.CommonOHOH:GetPath_Loop("UpperTorso.Head",character).Texture=head
					end)
				end

				PlayerCharacter[k]=character

				for _,fuc in pairs(v) do
					local s1,e1=xpcall(function()
						fuc(character:Clone())
					end,debug.traceback)
				end

				kId=k
				break
			end
			if kId then
				CallBackList[kId]=nil
				task.wait(0.2)
			else
				-- coroutine.close(UpdateConnect)
				UpdateConnect=nil
				return
			end
		end
	end)

	UpdateConnect()

end
function M.GetPlayerCharacter(userId,bk)
	assert(userId,"userId==null")
	if PlayerCharacter[userId] then
		if bk then
			bk(PlayerCharacter[userId]:Clone())
		end
	else
		if not CallBackList[userId] then
			CallBackList[userId]={}
		end
		if bk then
			table.insert(CallBackList[userId],bk)
		end
		if not UpdateConnect then
			M.Update()
		end
		
	end
end




return M